package com.acoffee.java.DynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author acoffee
 * @create 2021-11-15 13:57
 */
public class JdkDynamic {
//    public Object getProxyObject(Object targetClass){
//        Object obj = Proxy.newProxyInstance(targetClass.getClass().getClassLoader(), targetClass.getClass().getInterfaces(),
//                new InvocationHandler() {
//                    @Override
//                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                        LogService logService = new LogService();
//                        logService.beforePrint();
//                        Object invoke = method.invoke(proxy, args);
//                        logService.afterPrint();
//                        return invoke;
//                    }
//                }
//        );
//        return obj;
//    }
    public Object getProxyObject(Object targetClass){
        Object obj = Proxy.newProxyInstance(targetClass.getClass().getClassLoader(), targetClass.getClass().getInterfaces(),
                (Object proxy, Method method, Object[] args) -> {
                    LogService logService = new LogService();
                    logService.beforePrint();
                    Object invoke = method.invoke(targetClass, args);
                    logService.afterPrint();
                    return invoke;
                });
        return obj;
    }
}
